package com.manbearpig.todue;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;

/**
 * This method validates and stores/ updates todos in the db
 * @author Brandon Devereaux
 * @version 1.0
 *
 */
public class ToDo 
{

	private Date date;
	private Location location;
	private String description;
	private String shortdescription;
	private int recurrence; //0 if no recurrence
	private String category;
	private String completed;
	
	/**
	 * Creates a todo to hold the information
	 * @param d Date for the Todo
	 * @param l location for the Todo
	 * @param description Description of the Todo
	 * @param shortdescription Task Name/ Description
	 * @param recurrence 0 if no recurrence, anything else otherwise
	 * @param category The type of the todo
	 * @param completed "Completed!" if the todo is finished, "Not Completed" otherwise
	 */
	public ToDo(Date d, Location l, String description, String shortdescription, int recurrence, String category, String completed)
	{
		date=d;
		location=l;
		this.description=description;
		this.shortdescription=shortdescription;
		this.recurrence=recurrence;
		this.category=category;
		this.completed=completed;
	}
	
	/**
	 * Confirms that all data fields entered are correct with given restrictions:
	 * The date should be the current date or later
	 * all objects should be non-null
	 * @return a boolean describing whether all the given data is valid for a ToDo event
	 */
	public boolean isValid()
	{
		//add location check later
		/*if(date==null || description==null || shortdescription==null || category==""){
			return false;
		}
		if(date.compareTo(new Date())>=0){
			return false;
		}*/
		
		return true;
	}
	
	/**
	 * Creates the ToDo object in the database
	 * @return a boolean indicating whether the ToDo was created successfully
	 */
	public void createToDo(String username, Context c)
	{
		DatabaseHelper dbh = new DatabaseHelper(c);
		SQLiteDatabase db = dbh.getWritableDatabase();
		db.beginTransaction();
		ContentValues cv = new ContentValues();
		cv.put("Username", username);
		cv.put("Description", this.description);
		cv.put("Short_Description", this.shortdescription);
		SimpleDateFormat formatter = new SimpleDateFormat();
    	formatter.applyPattern("yyyy-MM-dd");//EEE MMM d, yyyy
    	String duedate = formatter.format(this.date);
		cv.put("Due_Date", duedate);
		cv.put("Recurrence", this.recurrence);
		cv.put("Category", this.category);
		cv.put("Location", "");
		cv.put("Completed", this.completed);
		db.insert("Todos", null, cv);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
		dbh.close();
	}
	
	/**
	 * Displays the ToDo's location on the map
	 */
	public void showMap()
	{
		
	}
	
	/**
	 * Updates all fields in the ToDo item.
	 * If the user doesn't change all the fields, the appropriate getters should be used as parameters
	 * @param d The date of the ToDo event
	 * @param l the Location of the ToDo event
	 * @param description A description of the ToDo event
	 * @param shortdescription a shorter version of the description for use in the list view
	 * @param recurrence how often the event recurs
	 * @param type Keywords so the user can filter ToDos
	 * @return whether the edit was successful
	 */
	public void editToDo(String username, Context c, int id)
	{
		DatabaseHelper dbh = new DatabaseHelper(c);
		SQLiteDatabase db = dbh.getWritableDatabase();
		db.beginTransaction();
		ContentValues cv = new ContentValues();
		cv.put("Username", username);
		cv.put("Description", this.description);
		cv.put("Short_Description", this.shortdescription);
		SimpleDateFormat formatter = new SimpleDateFormat();
    	formatter.applyPattern("yyyy-MM-dd");
    	String duedate = formatter.format(this.date);
		cv.put("Due_Date", duedate);
		cv.put("Recurrence", this.recurrence);
		cv.put("Category", this.category);
		cv.put("Location", "");
		cv.put("Completed", this.completed);
		db.update("Todos", cv, "_id=?", new String[]{((Integer)id).toString()});
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
		dbh.close();
	}
	

	public Date getDate()
	{
		return date;
	}
	
	public Location getLocation()
	{
		return location;
	}
	
	public String getDescription()
	{
		return description;
	}
	
	public String getShortDescription()
	{
		return shortdescription;
	}
	
	public int getRecurrence(){
		return recurrence;
	}
	
	public String getType(){
		return category;
	}
	
	public class Category
	{
		private String type;
		public Category(String t)
		{
			type = t;
		}
		public String getType()
		{
			return type;
		}
		public void setType(String t)
		{
			type = t;
		}
		
	}
}
